package org.shan.lis.utils;

import com.google.common.base.Ascii;

/**
 * Created by amanda.shan on 2019-07-05.
 */
public class AsciiUtils {

    public static String bytesToAscii(byte[] bytes, int offset, int dateLen) {
        if ((bytes == null) || (bytes.length == 0) || (offset < 0) || (dateLen <= 0)) {
            return null;
        }
        if ((offset >= bytes.length) || (bytes.length - offset < dateLen)) {
            return null;
        }

        StringBuilder builder = new StringBuilder();
        for (int i = offset; i < dateLen; i++) {
            switch (bytes[i]) {
                case Ascii.ENQ:
                    builder.append("<ENQ>");
                    break;
                case Ascii.ACK:
                    builder.append("<ACK>");
                    break;
                case Ascii.STX:
                    builder.append("<STX>");
                    break;
                case Ascii.ETX:
                    builder.append("<ETX>");
                    break;
                case Ascii.EOT:
                    builder.append("<EOT>");
                    break;
                default:
                    char c = (char) bytes[i];
                    builder.append(c);
            }
        }
        return builder.toString();
    }

    public static void main(String[] args) {
        char c = 55;
        System.out.println(c);
        c = Ascii.CR;
        System.out.println(c);
        c = 55;
        System.out.println(c);
    }
}
